package com.ruoyi.project.modules.category.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.modules.businessSystem.domain.BusinessSystem;
import com.ruoyi.project.modules.businessSystem.service.IBusinessSystemService;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.category.service.ICategoryService;
import com.ruoyi.project.modules.page.domain.Page;
import com.ruoyi.project.modules.page.service.IPageService;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.service.IUsageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类体系Controller
 *
 * @author smallrain
 * @date 2021-11-03
 */
@Controller
@RequestMapping("/category/category")
@Api(tags = "分类体系管理")
public class CategoryController extends BaseController {
    private String prefix = "category/category";

    @Autowired
    private ICategoryService categoryService;

    @RequiresPermissions("category:category:view")
    @GetMapping()
    public String category() {
        return prefix + "/category";
    }

    /**
     * 查询分类体系树列表
     */
    @RequiresPermissions("category:category:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Category> list(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        return list;
    }

    /**
     * 导出分类体系列表
     */
    @RequiresPermissions("category:category:export")
    @Log(title = "分类体系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "分类体系数据");
    }

    /**
     * 新增分类体系
     */
    @GetMapping(value = {"/add/{id}", "/add/"})
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("category", categoryService.selectCategoryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存分类体系
     */
    @RequiresPermissions("category:category:add")
    @Log(title = "分类体系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Category category) {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改分类体系
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Category category = categoryService.selectCategoryById(id);
        mmap.put("category", category);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类体系
     */
    @RequiresPermissions("category:category:edit")
    @Log(title = "分类体系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    @Autowired
    private IBusinessSystemService businessSystemService;

    @Autowired
    private IUsageService usageService;

    @Autowired
    private IPageService pageService;

    /**
     * 删除
     */
    @RequiresPermissions("category:category:remove")
    @Log(title = "分类体系", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        Category category = new Category();
        category.setParentId(id);
        List<Category> categories = this.categoryService.selectCategoryList(category);
        if (CollectionUtil.isNotEmpty(categories)) {
            return AjaxResult.warn("分类有子节点,请解除关联");
        }
        BusinessSystem businessSystem = new BusinessSystem();
        businessSystem.setCategoryId(id);
        List<BusinessSystem> businessSystems = this.businessSystemService.selectBusinessSystemList(businessSystem);
        if (CollectionUtil.isNotEmpty(businessSystems)) {
            return AjaxResult.warn("分类与业务系统有关联,请解除关联");
        }
        Usage usage = new Usage();
        usage.setCategoryId(id);
        List<Usage> usageList = this.usageService.selectUsageList(usage);
        if (CollectionUtil.isNotEmpty(usageList)) {
            return AjaxResult.warn("分类与用况有关联,请解除关联");
        }
        Page page = new Page();
        page.setCategoryId(id);
        List<Page> pages = pageService.selectPageList(page);
        if (CollectionUtil.isNotEmpty(pages)) {
            return AjaxResult.warn("分类与页面有关联,请解除关联");
        }
        return toAjax(categoryService.deleteCategoryById(id));
    }

    /**
     * 选择分类体系树
     */
    @GetMapping(value = {"/selectCategoryTree/{id}", "/selectCategoryTree/"})
    public String selectCategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("category", categoryService.selectCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 选择分类体系树
     */
    @GetMapping(value = {"/selectCategoryTree2/{id}/{type}"})
    public String selectCategoryTree2(@PathVariable(value = "id", required = false) Long id, @PathVariable(value = "type", required = false) String type, ModelMap mmap) {
        if (StringUtils.isNotNull(type)) {
            mmap.put("category", categoryService.selectCategoryById(id));
        }
        mmap.put("type", type);
        return prefix + "/tree2";
    }

    /**
     * 加载分类体系树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = categoryService.selectCategoryTree();
        return ztrees;
    }
    @GetMapping("/treeData2")
    @ResponseBody
    @ApiOperation(value = "获取页面分类数据")
    public List<Ztree> treeData2(@RequestParam(value = "type", required = false) String type) {
        List<Ztree> ztrees = categoryService.selectCategoryTree2(type);
        return ztrees;
    }

    @GetMapping("/treeDataForContainsObject")
    @ResponseBody
    public List<Ztree> treeDataForContainsObject(@RequestParam(value = "type", required = false) String type) {
        List<Ztree> ztrees = categoryService.selectCategoryTreeForContainsObject(type);
        return ztrees;
    }
}
